import { Article } from './search';

const initialState = {
  articles: {} as Articles,
  mark: { visible: false, article_id: '' } as ArticleMark,
  detail: {} as ArticleDetail,
};

// 定义文章action类型
export type ArticlesAction =
  | {
      type: 'article/saveArticles';
      payload: Articles;
    }
  | {
      type: 'article/setMark';
      payload: ArticleMark;
    }
  | {
      type: 'article/setArticleReport';
      payload: {
        target: string;
        type: string;
        remark: string;
        activeId: number;
      };
    }
  | {
      type: 'article/articleDetail';
      payload: ArticleDetail;
    }
  | {
      type: 'article/changeArticleDetail';
      // 将参数都变为可选，可以修改其中任意值
      payload: Partial<ArticleDetail>;
    }
  | { type: 'article/changeArticleDetailFollow' }
  | { type: 'article/changeArticleDetailLiking'; payload: any }
  | { type: 'article/changeArticleDetailCollect'; payload: any };

// 定义获取的文章返回值类型
export type Articles = {
  [channel_id: number]: {
    timestamp: number;
    list: Article[];
  };
};

// 定义获取文章列表的参数类型
export type getArticleParams = {
  channel_id: number;
  timestamp: number;
};

// 定义文章标记类型
export type ArticleMark = {
  visible: boolean;
  article_id: string;
};

// 定义文章详情类型
export type ArticleDetail = {
  art_id: string;
  title: string;
  pubdate: string;
  aut_id: string;
  content: string;
  aut_name: string;
  aut_photo: string;
  is_followed: boolean;
  is_collected: boolean;
  attitude: number;
  comm_count: number;
  like_count: number;
};

export default function articleReducer(
  state = initialState,
  action: ArticlesAction
): typeof initialState {
  switch (action.type) {
    // 获取文章列表
    case 'article/saveArticles':
      return {
        ...state,
        articles: action.payload,
      };

    // 设置文章标记
    case 'article/setMark':
      return {
        ...state,
        mark: action.payload,
      };

    // 举报文章(举报完删除文章)
    case 'article/setArticleReport':
      // console.log(69, action.payload);

      const articles = state.articles;
      const { list, timestamp } = articles[action.payload.activeId];
      return {
        ...state,
        articles: {
          // 保留其他频道的数据
          // ...articles,
          // 当前频道的不喜欢的那条文章过滤掉
          0: {
            timestamp,
            list: list.filter((v) => v.art_id !== action.payload.target),
          },
        },
      };

    // 获取文章详情
    case 'article/articleDetail':
      return {
        ...state,
        detail: action.payload,
      };

    // 修改文章详情（修改评论数量）
    case 'article/changeArticleDetail':
      const { comm_count } = state.detail;
      const newCommCount = comm_count + 1;
      return {
        ...state,
        detail: { ...state.detail, comm_count: newCommCount },
      };

    // 修改文章详情的关注
    case 'article/changeArticleDetailFollow':
      const { is_followed } = state.detail;
      return {
        ...state,
        detail: { ...state.detail, is_followed: !is_followed },
      };

    // 点赞文章和取消点赞
    case 'article/changeArticleDetailLiking':
      const { like_count } = state.detail;
      // console.log(141, action.payload);
      return {
        ...state,
        detail: {
          ...state.detail,
          attitude: action.payload.attitude === 1 ? 1 : -1,
          like_count:
            action.payload.attitude === 1 ? like_count + 1 : like_count - 1,
        },
      };

    // 收藏文章和取消收藏文章
    case 'article/changeArticleDetailCollect':
      return {
        ...state,
        detail: {
          ...state.detail,
          is_collected: action.payload.collect ? true : false,
        },
      };

    default:
      return state;
  }
}
